﻿<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.    
-->
    
<UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"
  x:Class="Microsoft.Windows.Controls.Samples.TreeViewEventsSample">
    <StackPanel>
        <!-- TreeView and TreeViewItem Events -->
        <ContentControl Content="TreeView and TreeViewItem Events" Style="{StaticResource Header}" />
        <StackPanel>
            <TextBlock Text="TreeView.SelectedItemChanged" Style="{StaticResource ApiName}" />
            <TextBlock Style="{StaticResource ApiDescription}">
                Occurs when the TreeView's SelectedItem changes.  The event
                arguments provide access to the old and new selected items.  The
                items are the actual objects provided to the Items or
                ItemsSource properties, not the TreeViewItems that wrap those
                objects (unless of course you provided actual TreeViewItems like
                the sample below).
            </TextBlock>
            
            <TextBlock Text="TreeViewItem.Selected" Style="{StaticResource ApiName}" />
            <TextBlock Style="{StaticResource ApiDescription}">
                Occurs when the IsSelected property of a TreeViewItem changes from false to true.
            </TextBlock>
            
            <TextBlock Text="TreeViewItem.Unselected" Style="{StaticResource ApiName}" />
            <TextBlock Style="{StaticResource ApiDescription}">
                Occurs when the IsSelected property of a TreeViewItem changes from true to false.
            </TextBlock>
            
            <TextBlock Text="TreeViewItem.Expanded" Style="{StaticResource ApiName}" />
            <TextBlock Style="{StaticResource ApiDescription}">
                Occurs when the IsExpanded property of a TreeViewItem changes from false to true.
            </TextBlock>
            
            <TextBlock Text="TreeViewItem.Collapsed" Style="{StaticResource ApiName}" />
            <TextBlock Style="{StaticResource ApiDescription}">
                Occurs when the IsExpanded property of a TreeViewItem changes from true to false.
            </TextBlock>
        </StackPanel>
        
        <!-- Event Viewer demo -->
        <ContentControl Content="Event Viewer" Style="{StaticResource Header}" />
        <TextBlock Style="{StaticResource Information}">
            Interact with the TreeView to see the events that it fires in the log below.
        </TextBlock>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="280" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            
            <!-- TreeView and TreeViewItems raising the events -->
            <controls:TreeView SelectedItemChanged="OnSelectedItemChanged">
                <controls:TreeViewItem Header="Controls" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed">
                    <controls:TreeViewItem Header="AutoComplete" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" />
                    <controls:TreeViewItem Header="Expander" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" />
                    <controls:TreeViewItem Header="NumericUpDown" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" />
                </controls:TreeViewItem>
                <controls:TreeViewItem Header="Layout" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed">
                    <controls:TreeViewItem Header="DockPanel" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" />
                    <controls:TreeViewItem Header="WrapPanel" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" />
                    <controls:TreeViewItem Header="Viewbox" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" />
                </controls:TreeViewItem>
                <controls:TreeViewItem Header="Charting" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed">
                    <controls:TreeViewItem Header="ColumnSeries" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" />
                    <controls:TreeViewItem Header="LineSeries" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" />
                    <controls:TreeViewItem Header="PieSeries" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" />
                </controls:TreeViewItem>
            </controls:TreeView>
            
            <!-- Event log used to display the events-->
            <ScrollViewer x:Name="EventViewer" Grid.Column="1" HorizontalScrollBarVisibility="Auto" Margin="5 0 0 0">
                <StackPanel x:Name="EventLog" />
            </ScrollViewer>
            <Button Grid.Row="1" Grid.Column="1" Content="Clear Log" Click="OnClearLog" Margin="0 5 0 0" HorizontalAlignment="Center" />
        </Grid>

        <src:SourceViewer xmlns:src="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <src:SourceFile Path="TreeViewEventsSample.xaml">
    <src:SourceFile.Source>
      <sys:String>&lt;!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.    
--&gt;
    
&lt;UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"
  x:Class="Microsoft.Windows.Controls.Samples.TreeViewEventsSample"&gt;
    &lt;StackPanel&gt;
        &lt;!-- TreeView and TreeViewItem Events --&gt;
        &lt;ContentControl Content="TreeView and TreeViewItem Events" Style="{StaticResource Header}" /&gt;
        &lt;StackPanel&gt;
            &lt;TextBlock Text="TreeView.SelectedItemChanged" Style="{StaticResource ApiName}" /&gt;
            &lt;TextBlock Style="{StaticResource ApiDescription}"&gt;
                Occurs when the TreeView's SelectedItem changes.  The event
                arguments provide access to the old and new selected items.  The
                items are the actual objects provided to the Items or
                ItemsSource properties, not the TreeViewItems that wrap those
                objects (unless of course you provided actual TreeViewItems like
                the sample below).
            &lt;/TextBlock&gt;
            
            &lt;TextBlock Text="TreeViewItem.Selected" Style="{StaticResource ApiName}" /&gt;
            &lt;TextBlock Style="{StaticResource ApiDescription}"&gt;
                Occurs when the IsSelected property of a TreeViewItem changes from false to true.
            &lt;/TextBlock&gt;
            
            &lt;TextBlock Text="TreeViewItem.Unselected" Style="{StaticResource ApiName}" /&gt;
            &lt;TextBlock Style="{StaticResource ApiDescription}"&gt;
                Occurs when the IsSelected property of a TreeViewItem changes from true to false.
            &lt;/TextBlock&gt;
            
            &lt;TextBlock Text="TreeViewItem.Expanded" Style="{StaticResource ApiName}" /&gt;
            &lt;TextBlock Style="{StaticResource ApiDescription}"&gt;
                Occurs when the IsExpanded property of a TreeViewItem changes from false to true.
            &lt;/TextBlock&gt;
            
            &lt;TextBlock Text="TreeViewItem.Collapsed" Style="{StaticResource ApiName}" /&gt;
            &lt;TextBlock Style="{StaticResource ApiDescription}"&gt;
                Occurs when the IsExpanded property of a TreeViewItem changes from true to false.
            &lt;/TextBlock&gt;
        &lt;/StackPanel&gt;
        
        &lt;!-- Event Viewer demo --&gt;
        &lt;ContentControl Content="Event Viewer" Style="{StaticResource Header}" /&gt;
        &lt;TextBlock Style="{StaticResource Information}"&gt;
            Interact with the TreeView to see the events that it fires in the log below.
        &lt;/TextBlock&gt;
        &lt;Grid&gt;
            &lt;Grid.RowDefinitions&gt;
                &lt;RowDefinition Height="280" /&gt;
                &lt;RowDefinition Height="Auto" /&gt;
            &lt;/Grid.RowDefinitions&gt;
            &lt;Grid.ColumnDefinitions&gt;
                &lt;ColumnDefinition Width="200" /&gt;
                &lt;ColumnDefinition /&gt;
            &lt;/Grid.ColumnDefinitions&gt;
            
            &lt;!-- TreeView and TreeViewItems raising the events --&gt;
            &lt;controls:TreeView SelectedItemChanged="OnSelectedItemChanged"&gt;
                &lt;controls:TreeViewItem Header="Controls" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed"&gt;
                    &lt;controls:TreeViewItem Header="AutoComplete" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" /&gt;
                    &lt;controls:TreeViewItem Header="Expander" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" /&gt;
                    &lt;controls:TreeViewItem Header="NumericUpDown" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" /&gt;
                &lt;/controls:TreeViewItem&gt;
                &lt;controls:TreeViewItem Header="Layout" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed"&gt;
                    &lt;controls:TreeViewItem Header="DockPanel" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" /&gt;
                    &lt;controls:TreeViewItem Header="WrapPanel" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" /&gt;
                    &lt;controls:TreeViewItem Header="Viewbox" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" /&gt;
                &lt;/controls:TreeViewItem&gt;
                &lt;controls:TreeViewItem Header="Charting" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed"&gt;
                    &lt;controls:TreeViewItem Header="ColumnSeries" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" /&gt;
                    &lt;controls:TreeViewItem Header="LineSeries" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" /&gt;
                    &lt;controls:TreeViewItem Header="PieSeries" Selected="OnSelected" Unselected="OnUnselected" Expanded="OnExpanded" Collapsed="OnCollapsed" /&gt;
                &lt;/controls:TreeViewItem&gt;
            &lt;/controls:TreeView&gt;
            
            &lt;!-- Event log used to display the events--&gt;
            &lt;ScrollViewer x:Name="EventViewer" Grid.Column="1" HorizontalScrollBarVisibility="Auto" Margin="5 0 0 0"&gt;
                &lt;StackPanel x:Name="EventLog" /&gt;
            &lt;/ScrollViewer&gt;
            &lt;Button Grid.Row="1" Grid.Column="1" Content="Clear Log" Click="OnClearLog" Margin="0 5 0 0" HorizontalAlignment="Center" /&gt;
        &lt;/Grid&gt;
    &lt;/StackPanel&gt;
&lt;/UserControl&gt;</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="TreeViewEventsSample.xaml.cs">
    <src:SourceFile.Source>
      <sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace Microsoft.Windows.Controls.Samples
{
    /// &lt;summary&gt;
    /// Sample demonstrating TreeView and TreeViewItem events.
    /// &lt;/summary&gt;
    [Sample("(3)Events", DifficultyLevel.Basic)]
    [Category("TreeView")]
    public partial class TreeViewEventsSample : UserControl
    {
        /// &lt;summary&gt;
        /// Initializes a new instance of the TreeViewEventsSample class.
        /// &lt;/summary&gt;
        public TreeViewEventsSample()
        {
            InitializeComponent();
        }

        /// &lt;summary&gt;
        /// Handle the TreeView.SelectedItemChanged event.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The TreeView.&lt;/param&gt;
        /// &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "The event handler is declared in XAML.")]
        private void OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs&lt;object&gt; e)
        {
            // Note: Our sample added actual TreeViewItems to the TreeView.Items
            // collection (instead of proding other CLR or business objects
            // directly to the Items or ItemsSource properties).  That means
            // e.OldValue and e.NewValue will be TreeViewItems instead of
            // strings, business objects, etc.
            TreeViewItem oldItem = e.OldValue as TreeViewItem;
            TreeViewItem newItem = e.NewValue as TreeViewItem;
            Log(string.Format(
                CultureInfo.CurrentUICulture,
                "TreeView: SelectedItemChanged from '{0}' to '{1}'",
                oldItem != null ? oldItem.Header as string : "(null)",
                newItem != null ? newItem.Header as string : "(null)"));
        }

        /// &lt;summary&gt;
        /// Handle the TreeViewItem.Selected event.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The TreeViewItem.&lt;/param&gt;
        /// &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "The event handler is declared in XAML.")]
        private void OnSelected(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            Log(item, "Selected");
        }

        /// &lt;summary&gt;
        /// Handle the TreeViewItem.Unselected event.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The TreeViewItem.&lt;/param&gt;
        /// &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "The event handler is declared in XAML.")]
        private void OnUnselected(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            Log(item, "Unselected");
        }

        /// &lt;summary&gt;
        /// Handle the TreeViewItem.Expanded event.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The TreeViewItem.&lt;/param&gt;
        /// &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "The event handler is declared in XAML.")]
        private void OnExpanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            Log(item, "Expanded");
        }

        /// &lt;summary&gt;
        /// Handle the TreeViewItem.Collapsed event.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The TreeViewItem.&lt;/param&gt;
        /// &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "The event handler is declared in XAML.")]
        private void OnCollapsed(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            Log(item, "Collapsed");
        }

        /// &lt;summary&gt;
        /// Add an event to the list of raised events on the demo.
        /// &lt;/summary&gt;
        /// &lt;param name="message"&gt;The message to log.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Called by an event handler declared in XAML.")]
        private void Log(string message)
        {
            // Add a new message to the event log
            message = string.Format(CultureInfo.CurrentUICulture, "[{0:hh:mm:ss}]  {1}", DateTime.Now, message);
            EventLog.Children.Add(new TextBlock { Text = message });

            // Scroll to the bottom of the event log
            EventViewer.ScrollToVerticalOffset(EventViewer.ExtentHeight);
        }

        /// &lt;summary&gt;
        /// Add an event to the list of raised events on the demo.
        /// &lt;/summary&gt;
        /// &lt;param name="item"&gt;The item that raised the event.&lt;/param&gt;
        /// &lt;param name="eventName"&gt;The name of the event to log.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Called by an event handler declared in XAML.")]
        private void Log(TreeViewItem item, string eventName)
        {
            Log(string.Format(
                CultureInfo.CurrentUICulture,
                "TreeViewItem '{0}': {1}",
                item != null ? item.Header as string : "(null)",
                eventName));
        }

        /// &lt;summary&gt;
        /// Clear the event log.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The Button.&lt;/param&gt;
        /// &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "The event handler is declared in XAML.")]
        private void OnClearLog(object sender, RoutedEventArgs e)
        {
            EventLog.Children.Clear();
        }
    }
}</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
</src:SourceViewer>
    </StackPanel>
</UserControl>